﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace EngineLayer.Common
{
    public class GenericProperty<T> : INotifyPropertyChanged
    {
        private T _value;

        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;

        protected void OnChanging(object sender)
        {
            if (PropertyChanging != null)
                PropertyChanging(this, new PropertyChangingEventArgs(sender.ToString()));
        }

        protected void OnValueChanged(object sender)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(sender.ToString()));
        }

        public T Value
        {
            get { return _value; }
            set
            {
                OnChanging(value);
                if (validations())
                {
                    _value = value;
                    OnValueChanged(value);
                }
                else
                    throw new Exception(Resources.ErrorMessages.AccessDenied);
            }
        }

        public static implicit operator T(GenericProperty<T> value)
        {
            return value.Value;
        }

        public static implicit operator GenericProperty<T>(T value)
        {
            return new GenericProperty<T> { Value = value };
        }

        public virtual bool validations() { return true; }
    }
   

}
